<?php
namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Activity;
use App\Models\Bill;
use App\Models\Recharge;
use App\Models\RechargeOrder;
use App\Models\User;
use App\Models\Wxpay;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Yansongda\Pay\Pay;

class RechargeController extends Controller{
    //充值套餐列表
    public function rechargeListApi(){
        try{
            $data = Recharge::select("id","image","money","score")->get();
            if ($data->count()<1){
                return self::error(1003);
            }
            return self::success($data);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //生成充值订单
    public function rehargeOrderApi(Request $request){
        try{
            $openid = $request->input("openid",false);
            $rechargeId = $request->input("recharge_id",false);
            if (!$openid || !$rechargeId)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            $recharge = Recharge::find($rechargeId);
            if (!$recharge)return self::error(1026);
            //开启事务
            DB::beginTransaction();
            //生成订单号
            $orderSn = 'CZ'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
            //生成充值订单
            $order = new RechargeOrder();
            $order->order_sn = $orderSn;
            $order->user_id = $user->id;
            $order->recharge_id = $rechargeId;
            $order->sell_id = $user->sell_id;
            $order->money = $recharge->money;
            $order->is_paid = 0;
            $order->order_status = 0;
            $order->order_time = Carbon::now();
            $order->save();

            //生成支付记录
            $pay = new Wxpay();
            $pay->userid = $user->id;
            $pay->location = 'recharge';
            $pay->pay_type = 'wechat';
            $pay->pay_sn = $orderSn;
            $pay->amount = $recharge->money;
            $pay->is_paid = 0;
            $pay->created_at = Carbon::now();
            $pay->save();

            //积分变动记录
            $bill = new Bill();
            $bill->order_sn = $orderSn;
            $bill->userid = $user->id;
            $bill->money = $recharge->score;
            $bill->type = 1;
            $bill->status = 0;
            $bill->created_at = Carbon::now();
            $bill->save();

            //微信支付
            $pay = Wxpay::wxPay();
            $orderPay = [
                'out_trade_no' => $orderSn,
                'total_fee' => $recharge->money*100, // **单位：分**
                'body' => "微信快捷支付",
                'openid' => $openid,
            ];
            $result = Pay::wechat($pay)->miniapp($orderPay);

            DB::commit();

            return self::success($result);
        }catch (\Exception $e){
            Log::error($e);
            DB::rollback();
            return self::error(500);
        }
    }

    //活动
    public function activityApi(){
        try{
            $activity = Activity::select("id","image","location")->get();
            return self::success($activity);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }


}